package com.ywg.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.ywg.entity.TrademarkOrder;
import com.ywg.service.AttorneyService;
import com.ywg.service.PaymentAccountService;
import com.ywg.service.PaymentOrderService;
import com.ywg.service.TrademarkOrderService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("excel")
public class ToExcelController {
    @Resource
    private PaymentOrderService paymentOrderService;
    @Resource
    private TrademarkOrderService trademarkOrderService;
    @Resource
    private AttorneyService attorneyService;
    @Resource
    private PaymentAccountService paymentAccountService;

    public static final Map<String, Integer> typeMap = new HashMap<>();

    static {
        typeMap.put("新申请", 1);
        typeMap.put("美标新申请", 1);
        typeMap.put("新申请（意向申请）", 1);
        typeMap.put("OA答复", 2);
        typeMap.put("ROA", 2);
        typeMap.put("删除小类", 2);
        typeMap.put("SOU补证", 3);
        typeMap.put("意向补证", 3);
        typeMap.put("意向补证（含变更律师）", 3);
        typeMap.put("变更地址", 4);
        typeMap.put("变更律师", 5);
        typeMap.put("5-6年宣誓", 6);
        typeMap.put("5-6宣誓", 6);
        typeMap.put("宣誓2件", 6);
        typeMap.put("美标转让", 7);
        typeMap.put("转让", 7);
        typeMap.put("美标转让(含变更律师)", 8);
        typeMap.put("第5-6年宣誓宽展期", 9);
        typeMap.put("宣誓", 9);
        typeMap.put("10年续展", 10);
    }

    @RequestMapping("test")
    public void toExcel(MultipartFile file) throws IOException, ParseException {
        //1.第一种 头必须和实体(英文)一样MultipartFile
        //文件处理成io流
        InputStream in = file.getInputStream();
        //io流给ExcelReader
        ExcelReader excelReader = ExcelUtil.getReader(in);
        List<String> sheetNames = excelReader.getSheetNames();
        for (String sheetName : sheetNames) {
            //忽略第一行头(第一行是中文的情况),直接读取表的内容
            List<List<Object>> list = excelReader.read(1);

            ArrayList<TrademarkOrder> tList = new ArrayList<>();

            for (List<Object> row : list) {
                TrademarkOrder trademarkOrder = new TrademarkOrder();
                //主键id
                trademarkOrder.setId(IdUtil.getSnowflakeNextId());
                //创建时间
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date dateObj = format.parse(row.get(0).toString());
                trademarkOrder.setCreateTime(dateObj);
                //tob表--事项
                trademarkOrder.setType(typeMap.get(row.get(1).toString()));
                trademarkOrder.setClientName(row.get(2).toString());
                //商标名
                trademarkOrder.setTrademarkName(row.get(3).toString());
                //注册号
                if (!row.get(4).toString().trim().equals("")) {
                    trademarkOrder.setSerialNumber(Long.parseLong(row.get(4).toString().trim()));
                } else {
                    trademarkOrder.setSerialNumber(0L);
                }
                //在哪位律师名下
                Long attId = attorneyService.getAttId(row.get(6).toString());
                trademarkOrder.setAttorneyId(attId == null ? 0 : attId);
                //刷谁的信用卡（先和娟确认）
                Long accId = paymentAccountService.getAccId(row.get(7).toString());
                trademarkOrder.setPaymentAccountId(accId == null ? 0 : accId);
                //申请人名字
                trademarkOrder.setApplicant(row.get(8).toString());
                //法人或负责人
                trademarkOrder.setApplicantSigner(row.get(9).toString());
                //邮箱号
                trademarkOrder.setApplicantEmail(row.get(10).toString());
                //客户类型
                trademarkOrder.setClientType(0);
                //币种
                trademarkOrder.setExpectedCurrency("CNY");
                //应收金额
                trademarkOrder.setExpectedAmount(BigDecimal.valueOf(0));
                //费用结算周期
                trademarkOrder.setSettlementPeriod(0);
                //官费币种
                trademarkOrder.setFeeCurrency("USD");
                //官费金额
                trademarkOrder.setFeeAmount(BigDecimal.valueOf(0));
                //备注
                trademarkOrder.setRemark("来源Excel历史数据");
                //汇率
                trademarkOrder.setExchangeRate(BigDecimal.valueOf(7.27));
                //提交人
                trademarkOrder.setSubmitUserId(trademarkOrderService.getUserIDByName(sheetName));

                tList.add(trademarkOrder);
            }
            //批量注册进数据库
            trademarkOrderService.saveBatch(tList);
        }
    }


//    @RequestMapping("test")
//    public void toExcel(MultipartFile file) throws IOException, ParseException {
//        //获取当前时间
//        Date date = new Date();
//
//        //1.第一种 头必须和实体(英文)一样MultipartFile
//        //文件处理成io流
//        InputStream in = file.getInputStream();
////        //io流给ExcelReader
//        ExcelReader excelReader = ExcelUtil.getReader(in);
////        //读取数据且转化为list
////        List<User> list = excelReader.readAll(User.class);
//
//        //2.第二种导入方式
//        //忽略第一行头(第一行是中文的情况),直接读取表的内容
//        List<List<Object>> list = excelReader.read(1);
//        ArrayList<PaymentOrder> pList = new ArrayList<>();
//        ArrayList<TrademarkOrder> tList = new ArrayList<>();
//
//        for (List<Object> row : list) {
//            PaymentOrder paymentOrder = new PaymentOrder();
//            TrademarkOrder trademarkOrder = new TrademarkOrder();
//            //主键id
//            trademarkOrder.setId(IdUtil.getSnowflakeNextId());
//            paymentOrder.setId(IdUtil.getSnowflakeNextId());
//            //po表--biz_id
//            paymentOrder.setBizId(trademarkOrder.getId());
//            //创建时间
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            Date dateObj = format.parse(row.get(0).toString());
//            paymentOrder.setCreateTime(dateObj);
//            trademarkOrder.setCreateTime(dateObj);
//            //tob表--事项
//            trademarkOrder.setType(typeMap.get(row.get(1).toString()));
//            //客户名称
//            paymentOrder.setClientName(row.get(2).toString());
//            trademarkOrder.setClientName(row.get(2).toString());
//            //商标名
//            trademarkOrder.setTrademarkName(row.get(3).toString());
//            //注册号
//            if (!row.get(4).toString().trim().equals("")){
//                trademarkOrder.setSerialNumber(Long.parseLong(row.get(4).toString().trim()));
//            }else {
//                trademarkOrder.setSerialNumber(0L);
//            }
//            //收款凭证
//            paymentOrder.setVoucher(row.get(5).toString());
//            //在哪位律师名下
//            Long attId = attorneyService.getAttId(row.get(6).toString());
//            trademarkOrder.setAttorneyId(attId==null?0:attId);
//            //刷谁的信用卡（先和娟确认）
//            Long accId = paymentAccountService.getAccId(row.get(7).toString());
//            trademarkOrder.setPaymentAccountId(accId==null?0:accId);
//            //申请人名字
//            trademarkOrder.setApplicant(row.get(8).toString());
//            //法人或负责人
//            trademarkOrder.setApplicantSigner(row.get(9).toString());
//            //邮箱号
//            trademarkOrder.setApplicantEmail(row.get(10).toString());
//            //创建时间
//            trademarkOrder.setCreateTime(date);
//            paymentOrder.setCreateTime(date);
//            //客户类型
//            trademarkOrder.setClientType(0);
//            //币种
//            trademarkOrder.setExpectedCurrency("CNY");
//            //应收金额
//            trademarkOrder.setExpectedAmount(BigDecimal.valueOf(0));
//            //费用结算周期
//            trademarkOrder.setSettlementPeriod(0);
//            //官费币种
//            trademarkOrder.setFeeCurrency("USD");
//            //官费金额
//            trademarkOrder.setFeeAmount(BigDecimal.valueOf(0));
//            //备注
//            trademarkOrder.setRemark("来源Excel历史数据");
//            //汇率
//            trademarkOrder.setExchangeRate(BigDecimal.valueOf(7.27));
//            //业务类型
//            paymentOrder.setBizType(0);
//            //付款方式
//            if (row.get(5).toString().contains("信用卡")){
//                paymentOrder.setMode(1);
//            } else if (row.get(5).toString().contains("支付宝")) {
//                paymentOrder.setMode(2);
//            } else if (row.get(5).toString().contains("银联")) {
//                paymentOrder.setMode(3);
//            } else if (row.get(5).toString().contains("微信")) {
//                paymentOrder.setMode(4);
//            }else {
//                paymentOrder.setMode(0);
//            }
//            //币种
//            paymentOrder.setCurrency("CNY");
//            //金额
//            paymentOrder.setAmount(BigDecimal.valueOf(0));
//            //付款凭证
//            paymentOrder.setVoucher(row.get(6).toString());
//            //收款主体
//            paymentOrder.setRecipient(1);
//
//            pList.add(paymentOrder);
//            tList.add(trademarkOrder);
//        }
//        //批量注册进数据库
//        trademarkOrderService.saveBatch(tList);
//        //paymentOrderService.saveBatch(pList);
//    }

}
